package com.eazytec.efs.api;

import com.eazytec.efs.common.Constants;
import com.eazytec.efs.common.DownBean;
import com.eazytec.efs.common.MyStringCallback;
import com.eazytec.efs.common.ResultMsg;
import com.eazytec.efs.utils.DownloadUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import java.io.File;

/**
 * EFS文件上传下载SDK接口
 *
 * @author Zhaozx
 * @create 2017-10-11 13:45
 **/
public class EFSUtils{

    private static final Logger logger = Logger.getLogger(EFSUtils.class);


    public static ResultMsg resultMsg = new ResultMsg();

    /**
     * 文件上传对外api
     * @param url
     * @param file
     * @param apikey
     * @return
     */
    public static ResultMsg upload(String url, String newName, File file, String apikey ) {
        logger.info("==============================================");
        logger.info("<<<<<<<<<<<<<<<<<<<<<          start upload...");

        if(file == null ){
            resultMsg.setCode(Constants.fail_code);
            resultMsg.setMsg(Constants.NO_FILE);
            return resultMsg;
        }
        if(StringUtils.isEmpty(apikey)){
            resultMsg.setCode(Constants.fail_code);
            resultMsg.setMsg(Constants.NO_APIKEY);
            return resultMsg;
        }

        if(!StringUtils.isNotEmpty(newName))
            newName = file.getName();

        OkHttpUtils.post()//
                .addFile("mFile", newName, file)//
                .url(url)
                .addHeader("apikey", apikey)
                .build()
                .execute(new MyStringCallback(resultMsg));

        logger.info(">>>>>>>>>>>>>>>>>>>>>          end upload...");
        logger.info("==============================================");
        return resultMsg;
    }

    public static ResultMsg downLoadFile(String url, String savePath) {
        logger.info("==============================================");
        logger.info("<<<<<<<<<<<<<<<<<<<<<          start downLoadFile...");
        if(StringUtils.isEmpty(url)){
            resultMsg.setCode(Constants.fail_code);
            resultMsg.setMsg(Constants.NO_URL);
            return resultMsg;
        }
        if(StringUtils.isEmpty(savePath)){
            resultMsg.setCode(Constants.fail_code);
            resultMsg.setMsg(Constants.NO_SAVEPATH);
            return resultMsg;
        }
        DownloadUtils.downLoad(url, savePath, resultMsg);
        logger.info(">>>>>>>>>>>>>>>>>>>>>          end downLoad...");
        logger.info("==============================================");
        return resultMsg;
    }

    public static ResultMsg pause(ResultMsg resultMsg) {
        logger.info("==============================================");
        logger.info("<<<<<<<<<<<<<<<<<<<<<          pause downLoadFile...");
        DownloadUtils.pause(resultMsg);
        logger.info(">>>>>>>>>>>>>>>>>>>>>          end pause...");
        logger.info("==============================================");
        return resultMsg;
    }

    public static ResultMsg continueDownload(ResultMsg resultMsg) {
        logger.info("==============================================");
        logger.info("<<<<<<<<<<<<<<<<<<<<<          continueDownload downLoadFile...");
        DownloadUtils.continueDownload(resultMsg);
        logger.info(">>>>>>>>>>>>>>>>>>>>>          end continueDownload...");
        logger.info("==============================================");
        return resultMsg;
    }

}
